<?php namespace app\system\controller;

use houdunwang\dir\Dir;
use houdunwang\request\Request;
use houdunwang\route\Controller;
use houdunwang\backup\Backup as BackupModel;

class Backup extends Controller
{

    //备份列表
    public function index()
    {
        $dirs = BackupModel::getBackupDir('backup');
        //		p($dirs);
        return view('', compact('dirs'));
    }

    /**
     * 执行备份
     */
    public function run()
    {
        $config = [
            'size' => 200,//分卷大小单位KB
            'dir' => 'backup/' . date("Ymdhis"),//备份目录
        ];
        $status = BackupModel::backup($config, function ($result) {
            //↑调用框架备份模块
            if ($result['status'] == 'run') {
                //备份进行中
                $message = $result['message'];
                die(view('message', compact('message')));
                //echo $result[ 'message' ];
                //↑刷新当前页面继续下次
            } else {
                //echo $result[ 'message' ];
                die($this->setRedirect('index')->success($result['message']));
                //↑备份执行完毕返回显示操作提示返回列表
            }
        }
        );
        if ($status === false) {
            //echo BackupModel::getError();
            die($this->error(BackupModel::getError()));
            //↑备份过程出现错误
        }
    }

    /**
     * 还原备份数据
     */
    public function recovery()
    {

        $config = ['dir' => Request::get('path')];
        //↑要还原的备份目录
        $status = BackupModel::recovery($config, function ($result) {
            //↑调用框架备份模块中还原功能
            if ($result['status'] == 'run') {
                //还原
                $message = $result['message'];
                die(view('message', compact('message')));
            } else {
                die($this->setRedirect('index')->success($result['message']));
                //↑还原执行完毕线束显示操作提示返回列表
            }
        }
        );
        if ($status === false) {
            //还原过程出现错误
            die($this->error(BackupModel::getError()));
        }
    }

    /**
     * 删除备份
     */
    public function del()
    {
        $path = Request::get('path');
        //删除备份目录
        Dir::del($path);

        return $this->setRedirect('index')->success('操作成功');
    }
}
